package frobagroup.pagination;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Tinsley on 2016/7/15.
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Pagination<T> {
    private int pageNumber;
    private int pageSize;
    private String sort;
    private String dir;
    private long total;
    private int totalPage;
    private List<T> content;
    private Map<String,String> conditions = new HashMap<String,String>();

    public Pagination(int pageNumber, int pageSize) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
    }

    public Pagination(int pageNumber, int pageSize, String sort) {
        this(pageNumber,pageSize);
        this.sort = sort;
    }
    public Pagination(long total, int totalPage, List<T> content) {
        this.total = total;
        this.totalPage = totalPage;
        this.content = content;
    }


    public int getOffset() {
        return Math.max(0,(getPageNumber() - 1) * getPageSize());
    }
    public void setTotal(long total) {
        this.total = total;
        this.totalPage = this.getPageSize() == 0?1:(int)Math.ceil((double)this.total / (double)this.getPageSize());
    }

}
